
try:
    import pygame
    from pygame import joystick
except:
    JOYSTICKENABLE = False
else:
    JOYSTICKENABLE = True
# event of joystick
from EventDefine import event_JOYSTICK_UP,event_JOYSTICK_DOWN,event_JOYSTICK_LEFT,event_JOYSTICK_RIGHT
from EventDefine import event_JOYSTICK_BTN0,event_JOYSTICK_BTN1,event_JOYSTICK_BTN2,event_JOYSTICK_BTN3
from EventDefine import event_JOYSTICK_BTN4,event_JOYSTICK_BTN5,event_JOYSTICK_BTN6,event_JOYSTICK_BTN7
# task of joystick polling
from TaskDefine import task_JOYSTICKPOLLING
# task for polling event of joystick
from direct.task import Task

myJoystick = None
# format of eventState: { nickname: [ state, eventName ] }
eventstate = {
    "up":[0,event_JOYSTICK_UP],
    "down":[0,event_JOYSTICK_DOWN],
    "left":[0,event_JOYSTICK_LEFT],
    "right":[0,event_JOYSTICK_RIGHT],
    0:[0,event_JOYSTICK_BTN0],
    1:[0,event_JOYSTICK_BTN1],
    2:[0,event_JOYSTICK_BTN2],
    3:[0,event_JOYSTICK_BTN3],
    4:[0,event_JOYSTICK_BTN4],
    5:[0,event_JOYSTICK_BTN5],
    6:[0,event_JOYSTICK_BTN6],
    7:[0,event_JOYSTICK_BTN7]
}

def JoyStickInit():
    if not JOYSTICKENABLE:
        print "Can not import pygame module"
        return False

    global myJoystick

    pygame.init()
    joystickCnt = joystick.get_count()
    if joystickCnt:
        myJoystick = joystick.Joystick(0)
        myJoystick.init()
        taskMgr.add(JoystickStatePolling, task_JOYSTICKPOLLING["name"], task_JOYSTICKPOLLING["sort"])
        ret = True
    else:
        ret = False

    return ret

def JoystickStatePolling(task):
    for e in pygame.event.get(): pass

    # scan current event state
    # 4 directions
    x = myJoystick.get_axis(0)
    y = myJoystick.get_axis(1)
    upPress, downPress = (y<-0.5), (y>0.5)
    leftPress, rightPress = (x<-0.5), (x>0.5)
    for (dirPress, nickname) in zip([upPress,downPress,leftPress,rightPress],["up","down","left","right"]):
        stateValue = eventstate[nickname]
        # event state change
        if stateValue[0] != dirPress:
            if dirPress:
                messenger.send(stateValue[1])
            else:
                messenger.send("%s-up"%(stateValue[1]))
            stateValue[0] = dirPress

    # 8 buttons
    for i in xrange(8):
        btnPress = myJoystick.get_button(i)
        stateValue = eventstate[i]
        # event state change
        if stateValue[0] != btnPress:
            if btnPress:
                messenger.send(stateValue[1])
            else:
                messenger.send("%s-up"%(stateValue[1]))
            stateValue[0] = btnPress

    return task.cont

def JoyStickDestroy():
    taskMgr.remove(task_JOYSTICKPOLLING["name"])


# for controller configure
from direct.showbase import DirectObject
from EventDefine import event_ALLCONTROLLEREVENT

class JoystickListener(DirectObject.DirectObject):
    def __init__(self):
        self.CreateAllJoystickEvent()
    def CreateAllJoystickEvent(self):
        event_JOYSTICKLIST = [event_JOYSTICK_UP,event_JOYSTICK_DOWN,event_JOYSTICK_LEFT,event_JOYSTICK_RIGHT,
                              event_JOYSTICK_BTN0,event_JOYSTICK_BTN1,event_JOYSTICK_BTN2,event_JOYSTICK_BTN3,
                              event_JOYSTICK_BTN4,event_JOYSTICK_BTN5,event_JOYSTICK_BTN6,event_JOYSTICK_BTN7]
        for eventName in event_JOYSTICKLIST:
            self.accept(eventName,self.eventHandle,[eventName])

    def eventHandle(self, eventName):
        messenger.send(event_ALLCONTROLLEREVENT, [eventName])

